<?php

class Core_Frontend_Module_Ad_Latest extends Dcmp_Module_Base {
	
    protected $name = 'ad_latest';

    public function install() {

    }

    public function uninstall() {

    }

    public function buildConfig($form) {
        $datas = App::getModel('promotion/collection_ad')->filterByStatus('published')->filterByTime(date('Y-m-d'))->load();
        $adPositions = App::getModel('promotion/collection_position')->load();
        $form->addSelectField($this->__('promotion.ad.position'), 'ad_position', 'module[parameters][ad_position]')
                ->bindData($adPositions, 'name', 'code');
        $form ->addSelectField($this->__('promotion.label.ad'),'custom_ad','module[parameters][custom_ad][]')
                ->setMultiple(TRUE);
        ob_start();
        include DP_DESIGN.'stable\frontend\default\module\ad\config.phtml';
        Kd_Web::getPage()->addScriptBlock(ob_get_clean());
        $form->createTab('module_option', $this->__('design.label.module_parameter'),
                array('field_ad_position', 'field_custom_ad'));
        $this->getTemplate()->assignMulti(array('parameters'=>$datas,'ad_position'=>$adPositions));
        
    }

    public function render() {
        $datas = App::getModel('promotion/collection_ad')
                ->joinPosition()
                ->filterByPosition($this->getValueByKey('ad_position'))
                ->filterByStatus('published')
                ->load();
        $tmpl = $this->getTemplate()->assignMulti(array('ads'=>$datas,'parameters'=>$this->getParameters()));
        return $tmpl->render($this->getTemplatePath($this->getValueByKey('template_name'),$this->getValueByKey('template_script')));
    }

}

